package com.apptravel.web.controller.admin;

import com.apptravel.dao.AreaDAO;
import com.apptravel.dao.HotelDAO;
import com.apptravel.dao.Page;
import com.apptravel.entity.Area;
import com.apptravel.entity.Hotel;
import com.apptravel.util.Constants;
import com.apptravel.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2012-1-16
 * Time: 11:43:46
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/admin/hotel")
public class AdminHotelController extends AdminBaseController{


    private static final String HOME_VIEW = "admin/hotel/home";

    private static final String EDIT_VIEW = "admin/hotel/edit";

    private static final String REDIRECT_VIEW ="/admin/hotel";

    private static final String UP = "/up";

    @Autowired
    private HotelDAO hotelDao;

    @Autowired
    private AreaDAO areaDao;

    /**
     * 首页列表.
     *
     * @param model
     * @return
     */
    @RequestMapping()
    public String home(ModelMap model,Integer page, Integer rows,String name,String area,Float gtprice,Float ltprice,Integer up){
        Map<String, Object> params = new HashMap<String, Object>();
        if (name != null && name.length() > 0) {
            params.put("name", "%" + name + "%");
            model.put("name", name);
        }
        if (area != null && area.length() > 0) {
            Map<String, Object> areaParams = new HashMap<String, Object>();
            areaParams.put("areaname", "%" + area + "%");
            List<Area> list = areaDao.getListNoPage(areaParams);
            String ids[] = new String[list.size()];
            for(int i=0;i<list.size();i++){
                ids[i] = list.get(i).getId().toString();
            }
            params.put("list",ids);
            areaParams.clear();areaParams = null;
            model.put("area",area);
        }
        if(gtprice != null && gtprice > 0){
            params.put("gtprice",gtprice);
            model.put("gtprice",gtprice);
        }
        if(ltprice != null && ltprice > 0){
            params.put("ltprice",ltprice);
            model.put("ltprice",ltprice);
        }
        if(up != null && up >-1 && up == 1){
            params.put("ispush",1);
            model.put("up",up);
        }
        if(up != null && up >-1 && up == 2){
            params.put("istop",1);
            model.put("up",up);
        }
        int pageNo = Constants.DEFAULT_PAGE;
        if (page != null && page > 0)
            pageNo = page;
        int pageSize = Constants.DEFAULT_PAGE_SIZE;
        if (rows != null && rows > 0)
            pageSize = rows;
        Page<Hotel> list = hotelDao.getList(params, pageNo, pageSize);
        model.put("page", list);

        return HOME_VIEW;
    }

    /**
     * 编辑.
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(EDIT)
    public String edit(ModelMap model,Long id){
        if(id != null && id > 0){
            Hotel obj = hotelDao.selectById(id);
            model.put("model",obj);
        }
        return EDIT_VIEW;
    }

    /**
     * 添加.
     *
     * @param model
     * @param hotel
     * @param response
     * @return
     */
    @RequestMapping(ADD)
    public String add(ModelMap model, Hotel hotel, HttpServletResponse response){
        hotelDao.insert(hotel);
        String html = this.makeResponseHtml(0, "");
        JsonUtil.renderHtml(html, response);
        return null;
    }

    /**
     * 更新.
     * @param hotel
     * @param model
     * @param response
     * @return
     */
    @RequestMapping(UPDATE)
    public String update(Hotel hotel,ModelMap model,HttpServletResponse response){
        hotelDao.updateById(hotel);
        String html = this.makeResponseHtml(0, "");
        JsonUtil.renderHtml(html, response);
        return null;
    }

    /**
     * 置顶/推介.
     * @param ids
     * @param
     * @param response
     * @return
     */
    @RequestMapping(UP)
    public String up(String ids,Integer top,Integer push,HttpServletRequest request,HttpServletResponse response){
        String[] idArray = ids.split(",");
        Map<String,Object> params = new HashMap<String,Object>();
		params.put("list", idArray);
        if(top != null){
            params.put("istop",top);
        }
        if(push != null){
            params.put("ispush",push);
        }
        hotelDao.updateByMap(params);
        String url = request.getContextPath()+REDIRECT_VIEW;
		String html = this.makeResponseHtml(1, url);
		JsonUtil.renderHtml(html, response);
        return null;
    }

    /**
     * 删除
     * @param ids
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(DEL)
    public String del(String ids, HttpServletRequest request,HttpServletResponse response){
        String[] idArray = ids.split(",");
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("list", idArray);
        hotelDao.deleteByMap(params);
        String url = request.getContextPath()+REDIRECT_VIEW;
		String html = this.makeResponseHtml(1, url);
		JsonUtil.renderHtml(html, response);
        return null;
    }


}
